package com.woyou.ui.activity;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import com.citaq.ideliver.R;
import com.woyou.bean.MyAddress;
import com.woyou.ui.adapter.AddrListAdapter;
import com.woyou.utils.eventbus.EventRefreshAddrList;

import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 我的地址界面MyAddrFragment
 * 
 * @author tanglc
 */
@EActivity(R.layout.fm_addrs)
public class MyAddrActivity extends BaseActivity {

	List<MyAddress> list;
	@ViewById
	RelativeLayout myback;
	@ViewById
	TextView head_title;
	@ViewById
	ListView addr_list;
	@ViewById
	Button addbtn;

	AddrListAdapter addrListAdapter;

	@AfterViews
	public void init() {
		initView();
	}
	
	/**
	 * 初始化View
	 */
	private void initView() {
		head_title.setText("我的地址");
		refreshListView();
	}

	@Override
	public void onResume() {
		super.onResume();
		refreshListView();
	}

	@Click({ R.id.myback, R.id.addbtn })
	void loginOnClick(View view) {
		switch (view.getId()) {
		case R.id.myback:
			// 退出地址管理界面
			finish();
			break;
			
		case R.id.addbtn:
			// 进入添加地址界面
			openActivity(PutAddrActivity_.class, null);
			break;
			
		default:
			break;
		}
	}

	/**
	 * 刷新我的地址页面
	 */
	public void onEvent(EventRefreshAddrList event) {
		refreshListView();
	}

	public void refreshListView() {
		list = mUserModel.queryAddrList();
		if (list != null && list.size() > 0) {
			addrListAdapter = new AddrListAdapter(list, mContext);
			addr_list.setAdapter(addrListAdapter);
		} else {
			list = new ArrayList<MyAddress>();
			addrListAdapter = new AddrListAdapter(list, mContext);
			addr_list.setAdapter(addrListAdapter);
		}
	}

	
	@Override
	public boolean backOperate() {
		// TODO Auto-generated method stub
		return false;
	}
	

}
